<?php
namespace app\index\controller;

use app\common\controller\Front;

class Text extends Front
{
    protected function initialize()
    {
        $this->auth['check'] = true;
        
        $this->auth['cancel_login'] = ['index/text/index'];
        
        $this->auth['cancel_limit'] = '*';

        $this->auth['redirect_url'] = dcUrl('index/text/index');
        
        parent::initialize(); 
    }

    public function index()
    {
        $this->site['historyGo'] = dcUrl('index/index/index');

        $this->assign(config('text.'));

        $this->assign($this->site);

        return $this->fetch();
    }

    public function save()
    {
        if(!config('apis.daicuo_token')){
            $this->error('请先配置阿呆API的TOKEN');
        }
        //解析接口
        $post = input('request.post/a');
        $json = model('index/Douyin','loglic')->init($post['text']);
        if(!$json){
            $this->error(model('index/Douyin','loglic')->getError());
        }
        //时长限制
        if($json['duration'] < intval(config('trans.min'))){
            $this->error('音频太短了，暂不支持');
        }elseif($json['duration'] > intval(config('trans.max'))){
            $this->error('音频太长了，暂不支持');
        }
        //积分验证
        $scoreTotal = config('text.score') * $json['duration'];
        if($scoreTotal){
            $this->scoreValidate($scoreTotal);
        }
        //识别结果
        if(!$text = model('index/Trans','loglic')->init($json['play'])){
            $this->error(model('index/Trans','loglic')->getError());
        }
        //扣除积分
        if($scoreTotal){
            model('common/Score','loglic')->decApps($this->user['user_id'], config('text.id'), $scoreTotal, config('text.name'));
        }
        //返回结果
        $this->success('ok','index/text/index',['desc'=>$json['title'],'duration'=>$json['duration'],'audio'=>$json['music'],'text'=>$text]);
    }
}